package com.gdk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gdk.service.LoginService;
//import com.qczj.utils.HttpSendUtil;
//import com.qczj.utils.IPUtils;

/**
 * 登录controller
 * */

@Controller
public class LoginController {
	
	@Autowired
    private LoginService loginService;
	
	
	/**
	 *	验证账号密码
	 * */
	@ResponseBody
	@RequestMapping("/loginVerify.do")
	public int loginVerify(@RequestParam Map<String,Object> map, HttpServletRequest request) {
	//	System.out.println("root : "+(Map<String,Object>)request.getSession().getAttribute("root"));
		map.put("root_status", 0);
		System.out.println("验证登录    map :"+map);
		Map<String, Object> rootDetails = loginService.doFindRootDetails(map);
	//	System.out.println("root   : "+rootDetails);
		if(rootDetails == null) {
			return 0;
		}else {
			/*int state = Integer.valueOf(rootDetails.get("root_status").toString());
			if(state == 0 ) {
				return -1;
			}*/
		}
		
		
		request.getSession().setAttribute("root", rootDetails);

		System.out.println("root : "+rootDetails);
		
		//Map<String, Object> address = IPUtils.getIPAndAddress();
		//String str = HttpSendUtil.sendGet("http://ip-api.com/json/?fields=520191&lang=zh-CN","utf-8");
		/*try {
			String str = HttpSendUtil.sendGet("http://pv.sohu.com/cityjson?ie=utf-8","utf-8");
			System.out.println("str : "+str);
			str = str.substring(str.indexOf("=")+1,str.lastIndexOf(";"));
			Gson gson = new Gson();
			Map<String, Object> ipMap = new HashMap<String,Object>();
			ipMap = gson.fromJson(str,ipMap.getClass());
			System.out.println("str : "+str);
			System.out.println("ipMap : "+ipMap);
			rootDetails.put("root_login_ip", ipMap.get("cip"));
			rootDetails.put("root_login_address",ipMap.get("cname"));
		}catch(Exception e) {
			rootDetails.put("root_login_ip","未获取");
			rootDetails.put("root_login_address","未获取");
		}*/
		rootDetails.put("root_login_ip", map.get("ip"));
		rootDetails.put("root_login_address", map.get("address"));
		
		//修改登录信息
		loginService.doUpdateLoginRootDetails(rootDetails);
		
		
		
		return 1;
	}
	/**
	 * 保存session 跳转主页
	 * */
	@RequestMapping("/toIndex.do")
	public String toIndex( HttpServletRequest request) {
//		System.out.println("主页root : "+(Map<String,Object>)request.getSession().getAttribute("root"));
		Map<String,Object> root = (Map<String, Object>) request.getSession().getAttribute("root");
		if(root ==null) {
			System.out.println("未登录");
			return "jsp/login";
		}else {
			System.out.println("已登录");
			
			Map<String,Object> questCountMap = new HashMap<String,Object>();
			
		    request.getSession().setAttribute("questCount", questCountMap);
			
			return "jsp/index/index";
		}
	}
	/**
	 *	退出
	 * */
	@ResponseBody
	@RequestMapping("/quitLogin.do")
	public int quitLogin( HttpServletRequest request) {
		System.out.println("root前 : "+(Map<String,Object>)request.getSession().getAttribute("root"));
		//清空session
		request.getSession().removeAttribute("root"); 
		System.out.println("root后 : "+(Map<String,Object>)request.getSession().getAttribute("root"));
		return 1;
	}
	
	
	
	

}